package test

import (
	"chat_gin/utils"
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
	"gorm.io/gorm/schema"
	"time"
)

func TestTime() {
	ConnectDb()
	GetData()
}

func ConnectDb() {
	dns := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		"root",
		"123456",
		"127.0.0.1",
		"3306",
		"hello",
	)
	fmt.Println(dns)
	db, err = gorm.Open(mysql.Open(dns), &gorm.Config{
		// gorm日志模式：silent
		//Logger: logger.Default.LogMode(logger.Silent), 不显示SQL
		Logger: logger.Default.LogMode(logger.Info), // 显示所有SQL
		// 外键约束
		DisableForeignKeyConstraintWhenMigrating: true,
		// 禁用默认事务（提高运行速度）
		SkipDefaultTransaction: true,
		NamingStrategy: schema.NamingStrategy{
			// 使用单数表名，启用该选项，此时，`User` 的表名应该是 `user`
			SingularTable: true,
		},
	})
	//err := db.Set("gorm:table_options", "ENGINE=InnoDB DEFAULT CHARSET=utf8")
	//if err != nil {
	//	fmt.Println("初始化user表失败", err)
	//	return
	//}
}

// ApplySpecialAuths 特殊门禁申请列表
type ApplySpecialAuths struct {
	ID             uint64    `gorm:"primaryKey;autoIncrement" json:"id"`
	TagIDs         string    `gorm:"type:varchar(256);not null" json:"tag_ids"`
	ApplyDesc      string    `gorm:"type:varchar(512);not null" json:"apply_desc"`
	SpecialID      uint64    `gorm:"type:bigint(20);not null" json:"special_id"`
	StaffName      string    `gorm:"type:varchar(512)" json:"staff_name"`
	DateRangeStart string    `gorm:"type:varchar(100)" json:"date_range_start"`
	DateRangeEnd   string    `gorm:"type:varchar(100)" json:"date_range_end"`
	ApplyReason    string    `gorm:"type:varchar(512)" json:"apply_reason"`
	CreateBy       string    `gorm:"type:varchar(64);not null" json:"create_by"`
	CreateTime     time.Time `gorm:"type:datetime;not null" json:"create_time"`
	UpdateBy       string    `gorm:"type:varchar(64);not null" json:"update_by"`
	UpdateTime     time.Time `gorm:"type:datetime;not null" json:"update_time"`
	SubIDs         string    `gorm:"type:varchar(64)" json:"sub_ids"`
	ApplyStatus    int8      `gorm:"type:tinyint(4)" json:"apply_status"`
	ApplyNumber    int       `gorm:"type:int(11)" json:"apply_number"`
}

func GetData() {
	var kk ApplySpecialAuths

	errr := db.Model(&kk).First(&kk).Error
	if errr != nil {
		fmt.Println(errr)
	}
	fmt.Println(kk)
	fmt.Println(kk.CreateTime)
	// 这里把go时间格式转出proto格式
	protoTime := utils.GoTimeToPbPtypes(kk.CreateTime)
	fmt.Println(protoTime)
}
